package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.CheckItem;
import com.itheima.entity.PageResult;
import com.itheima.entity.PageResult;
import com.itheima.pojo.CheckItem;
import com.itheima.entity.PageResult;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public List<Integer> findMemberCountByMonth(List<String> month) {
        List<Integer> list = new ArrayList<>();
        for (String m : month) {
            m = m + ".31";//格式：2019.04.31
            Integer count = memberDao.findMemberCountBeforeDate(m);
            list.add(count);
        }
        return list;
    }


    //会员编辑操作
    @Override
    public void edit(Member member) {
        memberDao.edit(member);
    }


    //会员添加 操作
    public void add(Member member) {
        String password = member.getPassword();
        if (password != null) {
            //使用md5将明文密码进行加密
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }
        memberDao.add(member);
    }

    //会员分页查询
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
            PageHelper.startPage(currentPage,pageSize);
            Page<Member> page=memberDao.selectByCondition(queryString);
            return new PageResult(page.getTotal(),page.getResult());
    }

    //会员查询   根据id
    @Override
    public Member findById(Integer id) {
        return memberDao.findById(id);
    }


    //会员删除操作
    @Override
    public void deleteById(Integer id) {
        /*long count = memberDao.findCountByCheckItemId(id);
        if(count>0){
            throw new RuntimeException("当前检查项被引用不能被删除");
        }*/
        memberDao.deleteById(id);
    }

    //会员查询全部操作
    public List<Member> findAll(){
        return memberDao.findAll();
    }
}